<?php
/*
 * 分析统计类
 */
namespace Home\Logic;

class AnalyzeLogic{
    
    /*活动订单类对象*/
    private $a;
    
    public function __construct() {
        $this->a = D('Action_order');
        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }
    
    /*订单总数*/
    public function countOrder($action_id){
        $re = $this->m->get('count_order_'.$action_id);
        if(!$re){
            $pay_order = $this->a->field('count(*) as c')->where('action_id='.$action_id.' and status=1')->find();
            $not_pay_order = $this->a->field('count(*) as c')->where('action_id='.$action_id.' and status=0')->find();
            $over_order = $this->a->field('count(*) as c')->where('action_id='.$action_id.' and status=2')->find();
            $re = array('pay'=>$pay_order['c'],'not_pay'=>$not_pay_order['c'],'over'=>$over_order['c']);
            $this->m->set('count_order_'.$action_id,$re,true);
        }
        return $re;
    }
    
    /*用户list*/
    public function getUserMobile($action_id){
        $re = $this->m->get('user_mobile_'.$action_id);
        if(!$re){
            $mobile_list = $this->a->field('distinct(mobile)')->where('action_id='.$action_id)->select();
            $re = $mobile_list;
            $this->m->set('user_mobile_'.$action_id,$re,true);
        }
        return $re;
    }
    
    public function getKeyUser($action_id){
        $re = $this->m->get('keyuser_mobile_'.$action_id);
        if(!$re){
            $sql = 'select distinct(mobile),count(*) as c from action_order where action_id='.$action_id.' and status>0 group by mobile having c>1';
            $mobile_list = $this->a->_read_data($this->a->query($sql));
            $this->m->set('keyuser_mobile_'.$action_id,$mobile_list);
        }
        return $mobile_list;
    }
    
    /*用户贡献榜*/
    public function getDevoteUser($action_id){
        $list = $this->m->get('devote_user_'.$action_id);
        if(!$list){
            $sql = 'select distinct(team_number) from action_order where action_id='.$action_id.' and status>0 group by team_number';
            $list = $this->a->_read_data($this->a->query($sql));
            if(count($list)>0){
                $_list = array();
                foreach($list as $v){
                    if($v['team_number']==''){
                        continue;
                    }
                    $_list[] = "'".$v['team_number']."'"; 
                }
                $list = implode(',',$_list);
                $sql = "select distinct(user_id),count(*) as c from marketing_team where type=1 and team_number in({$list}) group by user_id order by c desc";
                $list = $this->a->_read_data($this->a->query($sql));
                if($list[0]['user_id']>0){
                    //$_list = array();
                    foreach($list as $k=>$v){
                        $sql = "select mobile from client_user where id='{$v['user_id']}'";
                        $re = $this->a->_read_data($this->a->query($sql));
                        $list[$k]['mobile'] = $re[0]['mobile'];
                    }
                }
                $this->m->set('devote_user_'.$action_id,$list,true);
            }
        }
        return $list;
    }
    
    /*发型师贡献榜*/
    public function getDevoteBarber($action_id){
        $re = $this->m->get('devote_barber_'.$action_id);
        if(!$re){
            $sql = 'select distinct(barber_id),count(*) as c from action_order where action_id='.$action_id.' and status>0 group by barber_id having c>1 order by c desc';
            $re = $this->a->_read_data($this->a->query($sql));
            if(count($re)>0){
                $b = D('Barber','Logic');
                $list = array();
                foreach($re as $v){
                    $barber = $b->getBarber($v['barber_id']);
                    $list[] = array('name'=>$barber['name'],'c'=>$v['c']);
                }
                $re = $list;
            }
            $this->m->set('devote_barber_'.$action_id,$re,true);
        }
        return $re;
    }
    
    /*潜在用户*/
    public function getLatencyUser($action_id){
        $re = $this->m->get('latencyuser_'.$action_id);
        if(!$re){
            $sql = 'select distinct(mobile),count(*) as c from action_order where action_id='.$action_id.' and status=0 group by mobile';
            $mobile_list = $this->a->_read_data($this->a->query($sql));
            $key = array();
            $list = array();
            foreach($mobile_list as $v){
                if($v['c']>1){
                    $key[] = $v;
                }else{
                    $list[] = $v;
                }
            }
            $re = array('key'=>$key,'list'=>$list);
            $this->m->set('latencyuser_'.$action_id,$re,true);
        }
        return $re;
    }
    
    /*图表数据*/
    public function tableData($action_id){
        $re = $this->m->get('tabledata_'.$action_id);
        if(!$re){
            $actions = $this->a->field('create_time,status')->where('action_id='.$action_id)->order('create_time asc')->select();
            $pay = array();
            $not_pay = array();
            $over = array();
            $all = array();
            $_pay = array();
            $_not_pay = array();
            $_over = array();
            $_all = array();
            foreach($actions as $v){
                $day = date('Y-m-d',$v['create_time']);
                if($v['status'] == 1){
                    $pay[strtotime($day)] += 1;
                }else if($v['status']==0){
                    $not_pay[strtotime($day)] += 1;
                }else{
                    $over[strtotime($day)] += 1;
                }
                $all[strtotime($day)] += 1;
            }
            foreach($pay as $k=>$v){
                $_pay[] = array($k,$v);
            }
            foreach($not_pay as $k=>$v){
                $_not_pay[] = array($k,$v);
            }
            foreach($over as $k=>$v){
                $_over[] = array($k,$v);
            }
            foreach($all as $k=>$v){
                $_all[] = array($k,$v);
            }
            $re = array($_pay,$_not_pay,$_over,$_all);
            $this->m->set('tabledata_'.$action_id,$re,true);
        }
        return $re;
    }
    
}
    